//
//  ZhuihaojiluVC.m
//  SSC
//
//  Created by 红沙尘 on 17/6/11.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "ZhuihaojiluVC.h"
#import "ZhuihaojiluACell.h"
#import "ZhuihaojiluHeaderView.h"

@interface ZhuihaojiluVC ()
{
    NSMutableDictionary *viewDict;
    
    UITableView *zhuihaoTableView;
}

@property (nonatomic, assign) CaipiaoType otype;
@property (nonatomic, copy) NSString *titleStr;

@end

@implementation ZhuihaojiluVC

- (id)initWithType:(CaipiaoType)type title:(NSString *)title{
    self = [super init];
    
    if (self) {
        self.otype = type;
        self.titleStr = title;
    }
    
    return self;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    return [super initWithNibName:NSStringFromClass([self.superclass class]) bundle:nibBundleOrNil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [zhuihaoTableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:self.titleStr];
}

- (IBAction)searchButtonPressed {
    [self refreshNewData];
}

- (void)refreshNewData {
    page = 1;
    [self loadData];
}

- (void)loadData {
    
    [super loadData];
    
    WeakSelf
    __weak UITableView *weakTableView = zhuihaoTableView;
    
    [SSCRequest   userPlanBettListStartdt:[MTool formartDate:startField.text]
                                    enddt:[MTool formartDate:endField.text]
                                     page:page
                                    otype:self.otype
                                  success:^(NSDictionary *responseObject)
     {
         [weakTableView.mj_header endRefreshing];
         [weakTableView.mj_footer endRefreshing];
         [weakSelf loadWithArray:[NSString safeArray:responseObject[@"data"]]];
         
     } fail:^(NSError *error) {
         [weakTableView.mj_header endRefreshing];
         [weakTableView.mj_footer endRefreshing];
     }];

}

- (void)loadWithArray:(NSArray *)array {
    [super loadWithArray:array];
    
    [zhuihaoTableView reloadData];
}

#pragma mark - table

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    NSString *key = [NSString stringWithFormat:@"%d",(int)section];
    
    ZhuihaojiluHeaderView *view = viewDict[key];
    
//    if (!view)
    {
        view = [self headerViewAtSection:section];
        viewDict[key] = view;
    }
    
    return view.height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSString *key = [NSString stringWithFormat:@"%d",(int)section];
    
    return viewDict[key];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZhuihaojiluACell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZhuihaojiluACell"];
    [cell loadWithDict:dataArray[indexPath.section]];
    
    return cell;
}

- (void)setupData {
    [super setupData];
    
    viewDict = [[NSMutableDictionary alloc] init];
}

- (void)setupUI {
    [super setupUI];
    CGRect frame = CGRectMake(0, 214 + 12, ScreenWidth, ScreenHeight - 214 - 84 - 12);
    
    [mainTableView removeFromSuperview];
    mainTableView = nil;
    
    zhuihaoTableView = [[UITableView alloc] initWithFrame:frame
                                                    style:UITableViewStyleGrouped];
    zhuihaoTableView.delegate   = self;
    zhuihaoTableView.dataSource = self;
    zhuihaoTableView.backgroundColor = [UIColor whiteColor];
    zhuihaoTableView.separatorColor = [UIColor clearColor];
    [zhuihaoTableView registerNib:[UINib nibWithNibName:@"ZhuihaojiluACell" bundle:nil]
        forCellReuseIdentifier:@"ZhuihaojiluACell"];
    
    zhuihaoTableView.rowHeight = UITableViewAutomaticDimension;
    zhuihaoTableView.estimatedRowHeight = 60;
    
    [zhuihaoTableView reloadData];
    
    [self.view insertSubview:zhuihaoTableView belowSubview:[self noLabel]];
    
    WeakSelf
    
    zhuihaoTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadData];
    }];
    
    zhuihaoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf refreshNewData];
    }];
}

- (ZhuihaojiluHeaderView *)headerViewAtSection:(NSInteger)section {
    
    NSDictionary *dict = dataArray[section];
    
    NSString *issues = [NSString safeString:dict[@"issues"]];
    
    NSArray *array = [issues componentsSeparatedByString:@"|"];
    
    ZhuihaojiluHeaderView *view = [[[NSBundle mainBundle] loadNibNamed:@"ZhuihaojiluHeaderView" owner:nil options:nil] objectAtIndex:0];
    
    view.qiciLabel.text = [NSString stringWithFormat:@"追号期次：（共%d期）",(int)array.count];
    
    CGFloat contentH = 0;
    
    int count = (int)array.count;
    CGFloat y = 0;
    CGFloat x = 12;
    CGFloat width = (view.labelView.width - 24)/3;
    
    for (int i = 0; i < count; i ++) {
        
        int index = i%3;
        
        UILabel *label = [[UILabel alloc] init];
        label.width  = width;
        label.height = 40;
        label.top    = y;
        label.left   = x;
        label.font = [MTool fontOfSize:14];
        label.textColor = [UIColor colorWithHexString:@"#666666"];
        label.text = array[i];
        [view.labelView addSubview:label];
        
        x = label.right;
        y = label.top;
        
        if (index == 0) {
            label.textAlignment = NSTextAlignmentLeft;

        } else if (index == 1) {
            label.textAlignment = NSTextAlignmentCenter;
            
        } else if (index == 2) {
            label.textAlignment = NSTextAlignmentRight;
            x = 12;
            y = label.bottom;
        }
        
        if (i == count - 1) {
            contentH = label.bottom;
        }
    }
    
    view.labelView.height = contentH;
    
    view.height = contentH + 40;
    
    return view;
}

@end
